package com.hean.activity;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Main extends Activity {
	private ListView listView;
	private String[] mainOptions;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

//		Resources resources=this.getResources();
		mainOptions = getResources().getStringArray(R.array.main_options);

		listView = (ListView) findViewById(R.id.main);

		
		// ListAdapter adapter = new ArrayAdapter<String>(Main.this,
		// R.layout.main_lv, mainOptions);
		// lv.setAdapter(ListAdapter adapter);
		ListAdapter adapter = new ArrayAdapter<String>(Main.this,
				android.R.layout.simple_list_item_1, mainOptions);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String option = mainOptions[position];
				System.out.println(option);
				//类名按Activity01,02这样命名可以用反射
//				String className = "com.tarena.tts.Activity"
//					+ (position <= 9 ? "0" + position : "" + position);
//				Class class = Class.forName(className);
//				Intent intent = new Intent(Main.this, class);

				if (position == 0) {
					Intent intent = new Intent(Main.this, Phone.class);
					startActivity(intent);
				} else if (position == 1) {
					Intent intent = new Intent(Main.this, Image.class);
					startActivity(intent);
				}else if(position==2){
					Intent intent=new Intent(Main.this,MyMenu.class);
					startActivity(intent);
				}else if(position==3){
					Intent intent=new Intent(Main.this,ShowCube.class);
					startActivity(intent);
				}else if(position==4){
					Intent intent=new Intent(Main.this,WormGame.class);
					startActivity(intent);
				}else if(position==5){
					Intent intent=new Intent(Main.this,SensorActivity.class);
					startActivity(intent);
				}else if(position==6){
					Intent intent=new Intent(Main.this,SQLite.class);
					startActivity(intent);
				}else if(position==7){
					Intent intent = new Intent(Main.this,DIYAdapter.class);
					startActivity(intent);
				}else if(position==8){
					Intent intent = new Intent(Main.this,Net.class);
					startActivity(intent);
				}else if(position==10){
//					Intent intent=new Intent();
//					intent.setClass(Main.this, ParamsTransimt.class);//显示意图
////					intent.setComponent(new ComponentName(Main.this, Phone.class));//显示意图
//					intent.putExtra("id", 10);//向下一个activity传参数
//					intent.putExtra("name", "hean");
//					startActivityForResult(intent, 0);
					//下一个activity获取参数方法,在onCreate方法中
//					Intent intent = this.getIntent();
//					int id = intent.getIntExtra("id", 0);
//					String name=intent.getStringExtra("name");
					//关闭前返回数据
//					Intent intent = new Intent();
//					intent.putExtra(name, value);
//					NewActivity.this.setResult(RESULT_OK,intent);
//					this.finish();//明确关闭当前的activity，此方法调用可以增加一个按键；不调用则由操作系统来关闭
					//使用返回的数据要重写onActivityResult方法
					
					
					//隐式意图，意图对象本身并不知道要传递给哪个组件，至于传递给哪个意图是由android平台来决定的
//					Intent telIntent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:5556"));
//					startActivity(telIntent);//打开拨号器组件，然后拨打电话
					Intent intent=new Intent("com.hean.activity.hean",Uri.parse("file://data/data"));//Uri.parse("hean://www.hean.com/xxx")
					Bundle bundle=new Bundle();
					intent.setType("image/gif");//没有定义结构头，但是定义了文件类型，intent中写上Uri不配匹，而配匹类型
					bundle.putInt("id", 20);
					bundle.putString("name", "hean");
					intent.putExtras(bundle);
					startActivityForResult(intent, 0);
				}else if(position==11){
					Intent intent=new Intent(Main.this,GetXmlFromNet.class);
					startActivity(intent);
				}
				
				// Toast.makeText(Main.this, "你选择的是: " + option, 2000).show();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode==RESULT_OK){
			Toast.makeText(this, "返回的参数是："+data.getStringExtra("result"), 2000).show();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
